package oicon;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MyFrame extends JFrame {
    private JPanel left;
    private JPanel right;
    private JLabel xLabel;
    private JLabel yLabel;
    private JLabel rLabel;
    private JLabel colorLabel;
    private JTextField xTextField;
    private JTextField yTextField;
    private JTextField rTextField;
    private String[] color = {"Blue","Red","Green"};
    private JComboBox colorBox;
    private JButton drawButton;
    private DrawCircle draw;

    public MyFrame() {
        left = new JPanel();
        left.setLayout(new BorderLayout());
        right = new JPanel();
        JPanel temp = new JPanel();
        temp.setLayout(new GridLayout(4,2));

        xLabel = new JLabel("X     ");
        yLabel = new JLabel("Y     ");
        rLabel = new JLabel("R     ");
        colorLabel = new JLabel("Color     ");

        xTextField = new JTextField("");
        yTextField = new JTextField("");
        rTextField = new JTextField("");
        
        colorBox = new JComboBox(color);
        drawButton = new JButton("Draw");
        drawButton.addActionListener(new ButtonListener());
        draw = new DrawCircle();

        temp.add(xLabel);
        temp.add(xTextField);
        temp.add(yLabel);
        temp.add(yTextField);
        temp.add(rLabel);
        temp.add(rTextField);
        temp.add(colorLabel);
        temp.add(colorBox);

        draw.setBackground(Color.white);
        draw.setPreferredSize(new Dimension(175,175));

        left.add(temp,BorderLayout.NORTH);
        left.add(drawButton,BorderLayout.SOUTH);

        right.add(draw);

        Container contain = new Container();
        contain = getContentPane();
        contain.add(left,BorderLayout.WEST);
        contain.add(right,BorderLayout.EAST);

        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            double x = Double.parseDouble(xTextField.getText());
            double y = Double.parseDouble(yTextField.getText());
            double r = Double.parseDouble(rTextField.getText());
           
            String color = (String)colorBox.getSelectedItem();
            
            draw.setCircle(x,y,r,color);
        }
    }
    public class DrawCircle extends JPanel {
        double x;
        double y;
        double r;
        String color;

        public void setCircle(double x,double y,double r,String color) {
            this.x = x;
            this.y = y;
            this.r = r;
            this.color = color;
            draw.repaint();
        }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            Ellipse2D.Double circle = new Ellipse2D.Double(x,y,r,r);

            if (color == "Blue") {
                g2.setPaint(Color.blue);
                g2.fill(circle);
                g2.draw(circle);
            }
            else if (color == "Red") {
                g2.setPaint(Color.red);
                g2.fill(circle);
                g2.draw(circle);
            }
            else if (color == "Green") {
                g2.setPaint(Color.green);
                g2.fill(circle);
                g2.draw(circle);
            }
        }
    }
    public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				MyFrame m = new MyFrame();
			}
		});
	}
}
